package tech.wetech.admin3.controller;

import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tech.wetech.admin3.sys.model.AooUser;
import tech.wetech.admin3.sys.service.ScanLoginAliService;
import tech.wetech.admin3.sys.service.ScanLoginService;
import tech.wetech.admin3.util.AooJwtUtil;
import tech.wetech.admin3.util.CustomResponse;

@Slf4j
@RestController
@RequestMapping("/scan")
public class ScanLoginController {
  @Autowired
  private ScanLoginService scanLoginService;

  @Autowired
  private ScanLoginAliService scanLoginAliService;


  @Autowired
  private AooJwtUtil jwtUtil;

  @GetMapping("/getaddress")
  public String getAddress(HttpServletResponse response) throws Exception {
    return scanLoginService.getAddress();
  }

  @GetMapping("/getaddress_ali")
  public String getAddressAli() throws Exception {
    return scanLoginAliService.getAddressAli();

  }


  @PostMapping(value = "/wxLogin", produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<?> wxLogin(@RequestParam("code") String code) throws Exception {
    try {
      return ResponseEntity.ok(new CustomResponse.r1<>("登录成功", true, scanLoginService.wxlogin(code)));
    } catch (Exception e) {
      log.error("登录失败: {}", e.getMessage());
      return ResponseEntity.ok(new CustomResponse.r1("登录失败", false, e.getMessage()));
    }
  }

  @PostMapping("/aliLogin")
  public ResponseEntity<?> aliLogin(@RequestParam("code") String code) throws Exception {
    try {
      AooUser user = scanLoginAliService.alilogin(code);
      if(user == null) {
        return ResponseEntity.ok(new CustomResponse.r1<>("登录失败", false, null));
      }
      return ResponseEntity.ok(new CustomResponse.r1<>("登录成功", true, user));
    } catch (Exception e) {
      log.error("登录失败: {}", e.getMessage());
      return ResponseEntity.ok(new CustomResponse.r1("登录失败", false, e.getMessage()));
    }
  }


}
